package com.example.studentdemo.service.impl;

import com.example.studentdemo.entity.Subject;
import com.example.studentdemo.entity.SubjectStudent;
import com.example.studentdemo.entity.Student;
import com.example.studentdemo.exception.CustomerException;
import com.example.studentdemo.mapper.SubjectStudentMapper;
import com.example.studentdemo.mapper.SubjectMapper;
import com.example.studentdemo.mapper.StudentMapper;
import com.example.studentdemo.service.SubjectStudentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SubjectStudentServiceImpl implements SubjectStudentService {
    @Resource
    private SubjectStudentMapper subjectStudentMapper;

    @Resource
    private SubjectMapper subjectMapper;

    @Resource
    private StudentMapper studentMapper;

    @Override
    public List<SubjectStudent> selectAll(SubjectStudent subjectStudent) {
        return subjectStudentMapper.selectAll(subjectStudent);
    }

    @Override
    public PageInfo<SubjectStudent> selectPage(Integer pageNum, Integer pageSize, SubjectStudent subjectStudent) {
        // 开启分页查询
        PageHelper.startPage(pageNum, pageSize);
        List<SubjectStudent> subjectStudentList = subjectStudentMapper.selectAll(subjectStudent);
        return PageInfo.of(subjectStudentList);
    }

    @Override
    @Transactional
    public void addStudentToSubject(SubjectStudent subjectStudent) {
        // 检查课程是否存在
        Subject subject = subjectMapper.selectById(subjectStudent.getSubjectId());
        if (subject == null) {
            throw new CustomerException("课程不存在");
        }

        // 检查学生是否存在
        Student student = studentMapper.selectById(subjectStudent.getStudentId());
        if (student == null) {
            throw new CustomerException("学生不存在");
        }

        // 检查学生是否已经在课程中
        List<SubjectStudent> existingRecords = subjectStudentMapper.selectAll(subjectStudent);
        if (!existingRecords.isEmpty()) {
            throw new CustomerException("学生已经在该课程中");
        }

        subjectStudentMapper.insert(subjectStudent);
    }

    @Override
    @Transactional
    public void batchAddStudentsToSubject(Integer subjectId, List<Integer> studentIds) {
        // 检查课程是否存在
        Subject subject = subjectMapper.selectById(subjectId);
        if (subject == null) {
            throw new CustomerException("课程不存在");
        }

        for (Integer studentId : studentIds) {
            // 检查学生是否存在
            Student student = studentMapper.selectById(studentId);
            if (student == null) {
                throw new CustomerException("学生ID: " + studentId + " 不存在");
            }

            // 检查学生是否已经在课程中
            SubjectStudent temp = new SubjectStudent();
            temp.setSubjectId(subjectId);
            temp.setStudentId(studentId);
            List<SubjectStudent> existingRecords = subjectStudentMapper.selectAll(temp);
            if (!existingRecords.isEmpty()) {
                throw new CustomerException("学生ID: " + studentId + " 已经在该课程中");
            }

            // 添加学生到课程
            SubjectStudent subjectStudent = new SubjectStudent();
            subjectStudent.setSubjectId(subjectId);
            subjectStudent.setStudentId(studentId);
            subjectStudentMapper.insert(subjectStudent);
        }
    }

    @Override
    @Transactional
    public void removeStudentFromSubject(SubjectStudent subjectStudent) {
        // 检查记录是否存在
        List<SubjectStudent> existingRecords = subjectStudentMapper.selectAll(subjectStudent);
        if (existingRecords.isEmpty()) {
            throw new CustomerException("学生不在该课程中");
        }

        subjectStudentMapper.deleteBySubjectIdAndStudentId(subjectStudent);
    }

    @Override
    @Transactional
    public void batchRemoveStudentsFromSubject(Integer subjectId, List<Integer> studentIds) {
        for (Integer studentId : studentIds) {
            SubjectStudent temp = new SubjectStudent();
            temp.setSubjectId(subjectId);
            temp.setStudentId(studentId);

            // 检查记录是否存在
            List<SubjectStudent> existingRecords = subjectStudentMapper.selectAll(temp);
            if (existingRecords.isEmpty()) {
                throw new CustomerException("学生ID: " + studentId + " 不在该课程中");
            }

            subjectStudentMapper.deleteBySubjectIdAndStudentId(temp);
        }
    }

    @Override
    public List<SubjectStudent> selectBySubjectId(Integer subjectId) {
        return subjectStudentMapper.selectBySubjectId(subjectId);
    }

    @Override
    public List<SubjectStudent> selectByStudentId(Integer studentId) {
        return subjectStudentMapper.selectByStudentId(studentId);
    }

    @Override
    @Transactional
    public void deleteBySubjectId(Integer subjectId) {
        subjectStudentMapper.deleteBySubjectId(subjectId);
    }

    @Override
    @Transactional
    public void deleteByStudentId(Integer studentId) {
        subjectStudentMapper.deleteByStudentId(studentId);
    }
}